package edu.school.controller;

import com.github.pagehelper.PageInfo;
import edu.school.entity.BigData;
import edu.school.entity.Goods;
import edu.school.entity.User;
import edu.school.service.GoodsService;
import edu.school.service.UserService;
import edu.school.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Controller
public class GoodsController {
    @Autowired
    GoodsService service;
    @Autowired
    UserService us;
     @RequestMapping("/goods/toAdd")
    public String toAdd(){
        return "client/add_goods";
    }
    @RequestMapping(value = "/goods/list1",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList1(Goods record,HttpSession session,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){
        PageInfo<Goods> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        return "goods/list";

    }

    //分页查询失物信息
    @RequestMapping(value = "/goods/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String pageList(Goods record,HttpSession session,
                           @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                           ModelMap modelMap){
         User user=(User) session.getAttribute("user");
         if(user!=null){
             modelMap.addAttribute("url","goods");
             record.setU_id(user.getId());
       }
        PageInfo<Goods> pageInfo = service.getPageList(pageNum,pageSize,record);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("record",record);
        if(user!=null){
            return "/client/goods_list";
        }
        return "goods/list";

    }

    //发布失物信息
    @RequestMapping(value = "/goods/add",method = RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String save(Goods record,HttpSession session){
         User user=(User) session.getAttribute("user");
        if(user==null){
            return "<script>alert('请先登录');location.href='/toLogin.action';</script>";
        }
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        record.setCreate_time(timeStamp);
        record.setStatus(String.valueOf(0));
        record.setU_id(user.getId());
        if(service.save(record)){
            return "<script>alert('发布成功');location.href='/goods/list';</script>";
        }else{
            return "<script>alert('添加失败');history.go(-1);</script>";
        }

    }
    //修改失物信息
    @RequestMapping(value = "/goods/edit",method =RequestMethod.POST,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String edit(Goods record, HttpSession session){
        if(service.update(record)){
            User user=(User) session.getAttribute("user");
            return "<script>alert('修改成功');location.href='/goods/list';</script>";
        }else{
            return "<script>alert('修改失败');history.go(-1);</script>";
        }

    }

    //回显失物信息
    @RequestMapping(value = "/goods/query")
    public String query(Integer id, Model model,HttpSession session){
        Goods record=service.findById(id);
        if(null!=record){
            model.addAttribute("record",record);
        }
        return "goods/edit";
    }
    @RequestMapping(value = "/goods/qtquery")
    public String qtquery(Integer id, Model model,HttpSession session){
        Goods record=service.findById(id);
        if(null!=record){
            model.addAttribute("record",record);
        }

        model.addAttribute("url","goods");
        return "/client/edit_goods";
    }
    //失物详情
    @RequestMapping(value = "/goods/detail")
    public String detail(Integer id, Model model){
        Goods record=service.findById(id);
        if(null!=record){
            model.addAttribute("record",record);

        }
        User user=us.findById(record.getU_id());
        if(null!=user){
            model.addAttribute("user",user);
        }
        return "client/goods_detail";
    }


    @RequestMapping("/goods/delete")
    @ResponseBody
    public String ajaxdelete(Integer id,String filename) {
        boolean b =service.deleteGoods(id);
        System.out.println(filename+"文件名称");
        WebUtils.deleteImg(filename);
        if(b){
            return "ok";
        }
        return "fail";

    }

    //批量删除
    @RequestMapping("/goods/deletemany")
    @ResponseBody
    public String  deletemany(String ids) {
        System.err.println(ids);
        String[] arrayids=ids.split(",");//把数组里的值逗号隔开
        System.out.println("批量删除成功");
        for (int i=0;i<arrayids.length;i++){
            Goods goods=service.findById(Integer.parseInt(arrayids[i]));
            WebUtils.deleteImg(goods.getGoods_img());
        }
        service.deleteMany(arrayids);
        return "ok";//返回给ajax
    }
//    认领统计
@RequestMapping(value = "/goods/data")
@ResponseBody
public List<BigData> getBigDataList(){
    List<BigData> list = service.selectBigData();
    return list;
}
}
